import * as types from '../constants/ActionTypes';

export default function user(state = [], action = {}) {
  const {
        type, payload
    } = action;

  switch (type) {
        case types.FETCH_USER:
            return {...payload};
        case types.FETCH_USER_SUCCESS:
            let userSuccessInfo = null;
            if (payload.results && payload.results.length) {
                userSuccessInfo = payload.results[0];
            }else{
                userSuccessInfo =  {
                    user_status: -1,
                    message: "获取用户信息失败"
                };
            }
            return userSuccessInfo;
        case types.FETCH_USER_FAILURE:
            let userFailureInfo = {
                user_status : -1,
                message : "获取用户信息失败"
            };

            return userFailureInfo;

        case types.UPDATE_USER_INFO:
            let updateUserInfo = {};
            updateUserInfo[payload.key] = payload.value;

        return {...state, ...updateUserInfo};

        //==============================================================
        case types.SAVE_DEVICEINFO_SUCCESS:
            let successInfo = null;
            if (payload.results && payload.results.length) {
                successInfo = {
                    device_status : 1,
                    message : "保存用户设备信息成功"
                };
            }else{
                successInfo =  {
                    device_status: -1,
                    message: "保存用户设备信息失败"
                };
            }
            return {
                ...state,
                ...successInfo
            };
        case types.SAVE_DEVICEINFO_FAILURE:
            let failureInfo = {
                device_status: -1,
                message: payload.message||'网络连接失败'
            };
            return {
               ...state,
               ...failureInfo
            }; 

        //==============================================================
        case types.RESET_USER_INFO:
            return (payload || []);
        default:
            return state;
    }
}
